﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Text;
using System.ServiceModel;

namespace Spike.ServiceFramework.ServiceModel
{
    public class FrameworkChannelFactory<T> : ChannelFactory<T>
    {
        public ClientBehaviorExtension ClientBehaviorExtension { get; set; }

        public FrameworkChannelFactory(Binding binding) : base(binding)
        {
        }

        public FrameworkChannelFactory(ServiceEndpoint endpoint) : base(endpoint)
        {
        }

        public FrameworkChannelFactory(string endpointConfigurationName) : base(endpointConfigurationName)
        {
        }

        public FrameworkChannelFactory(Binding binding, EndpointAddress remoteAddress) : base(binding, remoteAddress)
        {
        }

        public FrameworkChannelFactory(Binding binding, string remoteAddress) : base(binding, remoteAddress)
        {
        }

        public FrameworkChannelFactory(string endpointConfigurationName, EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress)
        {
        }

        protected override void OnOpening()
        {
            if (!Endpoint.Behaviors.Any(b => b is ClientMessageInspector))
                Endpoint.Behaviors.Add(new ClientMessageInspector(ClientBehaviorExtension));

            base.OnOpening();
        }
    }
}